<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" size="medium" class="ry_form">
      <el-form-item label="分类编码" prop="number">
        <el-input
          v-model.trim="queryParams.number"
          placeholder="请输入分类编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类名称" prop="name">
        <el-input
          v-model.trim="queryParams.name"
          placeholder="请输入分类名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="父节点" prop="parentId">
        <el-input
          v-model.trim="parentName"
          disabled
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="显示排序" prop="orderNo">
        <el-input
          v-model.trim="queryParams.orderNo"
          placeholder="请输入显示排序"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="flex_one tr">
        <el-button  size="mini" @click="handleQuery">搜索</el-button>
        <el-button  size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          plain
         
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:AssetCategory:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:AssetCategory:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:AssetCategory:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['wms:AssetCategory:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <vxe-table v-loading="loading" :data="AssetCategoryList" @selection-change="handleSelectionChange">
      <vxe-column type="checkbox" width="55" align="center"></vxe-column>
      <vxe-column field="number" title="分类编码" align="center" v-if="columns[0].visible"></vxe-column>
      <vxe-column field="name" title="分类名称" align="center" v-if="columns[1].visible"></vxe-column>
      <vxe-column field="parentId" title="父节点ID" align="center" v-if="columns[2].visible"></vxe-column>
      <vxe-column field="orderNo" title="显示排序" align="center" v-if="columns[3].visible"></vxe-column>
      <vxe-column field="remark" title="备注" align="center" v-if="columns[4].visible"></vxe-column>
      <vxe-column title="操作" align="center" width="150">
        <template #default="{ row }">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(row)"
            v-hasPermi="['wms:AssetCategory:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(row)"
            v-hasPermi="['wms:AssetCategory:remove']"
          >删除</el-button>
        </template>
      </vxe-column>
    </vxe-table>
    <div class="pagination-container">
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
</div>
    <!-- 添加或修改资产分类表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="108px" inline class="dialog-form-two">
        
        <el-form-item label="分类名称" prop="name">
          <el-input v-model.trim="form.name" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="显示排序" prop="orderNo">
          <el-input v-model.trim="form.orderNo" placeholder="请输入显示排序" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model.trim="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAssetCategory, getAssetCategory, delAssetCategory, addAssetCategory, updateAssetCategory, exportAssetCategory } from "@/api/asset/assetCategory.js";

export default {
  name: "AssetCategory",
  props: {
    parentNode: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产分类表表格数据
      AssetCategoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        number: null,
        name: null,
        parentId: null,
        orderNo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
      },
      columns: [
            { key: 1, label: "分类编码", visible:  true  },
            { key: 2, label: "分类名称", visible:  true  },
            { key: 3, label: "父节点ID", visible:  true  },
            { key: 4, label: "显示排序", visible:  true  },
            { key: 5, label: "备注", visible:  true  },
             ],
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 查询资产分类表列表 */
    getList() {
      this.loading = true;
      const {pageNum, pageSize} = this.queryParams;

      if (this.parentNode) {
        this.queryParams.parentId = this.parentNode.id;
        this.queryParams.parentNumber = this.parentNode.number;
      }
      
      
      const query = {...this.queryParams, pageNum, pageSize};
      const pageReq = {page: pageNum - 1, size: pageSize};
      listAssetCategory(query, pageReq).then(response => {
        const { content, totalElements } = response
        this.AssetCategoryList = content;
        this.total = totalElements;
        this.loading = false;
      }).catch(error => {
        this.loading = false;
        console.log(error);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        number: null,
        name: null,
        parentId: null,
        orderNo: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资产分类表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAssetCategory(id).then(response => {
        this.form = response;
        this.open = true;
        this.title = "修改资产分类表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (this.parentNode) {
          this.form.parentId = this.parentNode.id;
          this.form.parentNumber = this.parentNode.number;
        }
        
        if (valid) {
          if (this.form.id != null) {
            updateAssetCategory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.$emit('sub');
            });
          } else {
            addAssetCategory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.$emit('sub');
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除资产分类表编号为"' + ids + '"的数据项？').then(function() {
        return delAssetCategory(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有资产分类表数据项？').then(() => {
        this.exportLoading = true;
        return exportAssetCategory(queryParams);
      }).then(response => {
        this.$download.download(response);
        this.exportLoading = false;
      }).catch(() => {});
    }
  },
  computed: {
    parentName: function() {
      return this.parentNode ? this.parentNode.name : '';
    }
  },
  watch: {
    parentNode: function(val) {
      this.getList();
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
  height: 92vh; 
  overflow: hidden; 
}

.vxe-table {
  flex: 1;
  overflow: auto;
}

.pagination-container {
  display: flex;
  justify-content: flex-end; 

}
</style>

